function initPage(pageNo,totalCount,arr){
    // 获取分页器实例对象
    var myPager = $('#myPager').data('zui.pager');
    //设置当前页码为 4，并同时设置记录总数为 100， 每页记录数目为 30
    myPager.set(pageNo, totalCount, 5);
    renderUserList(arr);
}
$('#myPager').pager({
    onPageChange: function(state, oldState) {
        if (state.page !== oldState.page) {
            // console.log('页码从', oldState.page, '变更为', state.page);
            queryBtn(state.page);

        }
    }
});
function renderUserList(arr){
    let str = '';
    for (let i = 0; i < arr.length; i++) {
        let a  = arr[i];
        var sta = '';
        var status1 = '';
        if (a.status==1){
            sta ="启用";
            status1 = "禁用"
        }else if(a.status == 3){
            sta ="禁用";
            status1 = "启用"
        }else{
            sta ="已删除";
            status1 = "已删除"
        }
        if (a.status == 2){
            str += `<tr>
                    <td>${a.id}</td>
                    <td>${a.name}</td>
                    <td>${a.positionalTitles}</td>
                    <td>${a.school}</td>
                    <td>${sta}</td>
                    <td>
                        <button onclick="updBtn(${a.id},'${a.name}',${a.status})" type="button" data-toggle="modal" data-target="#myModal" id="updateBtn"  class="btn btn-info disabled updateBtn btn-primary">${status1}</button>
                        <button onclick="del(${a.id},'${a.name}')" data-toggle="modal" data-target="#myModal1" id="delBtn" class="delBtn btn btn-danger disabled btn-primary" >删除</button>
                        <button onclick="remake(${a.id},'${a.name}')" class="delBtn disabled btn btn-danger btn-primary" data-toggle="modal" data-target="#myModal2" >重置密码</button>
                    </td>
                </tr>`;
        }else {
            str += `<tr>
                    <td>${a.id}</td>
                    <td>${a.name}</td>
                    <td>${a.positionalTitles}</td>
                    <td>${a.school}</td>
                    <td>${sta}</td>
                    <td>
                        <button onclick="updBtn(${a.id},'${a.name}',${a.status})" type="button" data-toggle="modal" data-target="#myModal" id="updateBtn"  class="btn btn-info updateBtn btn-primary">${status1}</button>
                        <button onclick="del(${a.id},'${a.name}')" data-toggle="modal" data-target="#myModal1" id="delBtn" class="delBtn btn btn-danger btn-primary" >删除</button>
                        <button onclick="remake(${a.id},'${a.name}')" class="delBtn btn btn-danger btn-primary" data-toggle="modal" data-target="#myModal2" >重置密码</button>
                    </td>
                </tr>`;
        }

    }
    $('#tbody')[0].innerHTML = str;
}

function updBtn(id,username,status){
    if (status == 3){
        $(".modal-body-p")[0].innerHTML = "确认启用用户 <span style='color: red'>" + username + "</span> ?";
    }else if (status == 1){
        $(".modal-body-p")[0].innerHTML = "确认禁用用户 <span style='color: red'>" + username + "</span> ?";
    }
    $(".modal-body-p").val(id);
}

function onDetermine(){
    let val = $(".modal-body-p").val();
    console.log(val);
    $.ajax({
        url:config.url + "/cons/updateStatus",
        dataType:'json',
        xhrFields: {
            withCredentials: true
        },
        data:{
            id:$(".modal-body-p").val()
        },
        crossDomain: true,
        type:'GET' ,
        success:function(resp) {
            location.reload(true);
        },
        error:function() {
        }
    });
}

function del(id,name){
    $(".modal-body-d")[0].innerHTML = "确认删除用户 <span style='color: red'>" + name + "</span> ?";
    $(".modal-body-d").val(id);
}
function onDetermine1(){
    $.ajax({
        url:config.url + "/cons/del",
        dataType:'json',
        xhrFields: {
            withCredentials: true
        },
        data:{
            id:$(".modal-body-d").val()
        },
        crossDomain: true,
        type:'GET',
        success:function(resp) {
            location.reload(true);
        },
        error:function() {
        }
    });
}
function remake(id,name){
    console.log(666,id);
    $("#modal-body-h")[0].innerHTML = "确认将用户 <span style='color: red'>" + name + "</span> 的密码重置?";
    $("#modal-body-h").val(id);
}
function onDetermine2(){
    console.log($("#modal-body-h").val());
    $.ajax({
        url:config.url + "/cons/remake",
        dataType:'json',
        xhrFields: {
            withCredentials: true
        },
        data:{
            id:$("#modal-body-h").val()
        },
        crossDomain: true,
        type:'GET',
        success:function(resp) {
            location.reload(true);
        },
        error:function() {
        }
    });
}

$(document).ready(function() {
    $("#input-check").on('click', function(){
        clickSwitch()
    });

    var clickSwitch = function() {
        if ($("#input-check").is(':checked')) {
            $("#check-name")[0].innerHTML = "封禁";
        } else {
            $("#check-name")[0].innerHTML = "开启";
        }
    };
});
$(document).ready(function() {
    $("#input-check1").on('click', function(){
        clickSwitch()
    });

    var clickSwitch = function() {
        if ($("#input-check1").is(':checked')) {
            $("#check-name1")[0].innerHTML = "咨询师";
        } else {
            $("#check-name1")[0].innerHTML = "管理员";
        }
    };
});
function selectInput(){
    if ($("#input-check1").is(':checked')) {
        $('.none').css('display','flex');
        console.log("咨询师")
    } else {
        $('.none').css('display','none');
        console.log("管理员")
    }
}


queryBtn(1);
function queryBtn(i){
    let username = $("#use-input").val();
    let status = "";
    if ($("#input-check").is(':checked')) {
        status = 3;
    } else {
        status = 1;
    }
    // $('#picker3').val()
    $.ajax({
        url:config.url + "/cons/queryForCondition",
        dataType:'json',
        xhrFields: {
            withCredentials: true
        },
        data:{
            username:username,
            status:status,
            positionalTitles:$('#picker3').val(),
            pageNo:i
        },
        crossDomain: true,
        type:'POST',
        success:function(resp) {
            var totalCount = resp.data.totalCount;
            renderUserList(resp.data.list);
            initPage(resp.pageNo,totalCount,resp.data.list);
        },
        error:function() {
        }
    });
}
function removeI(){
    $('#use-input').val("");
}
getpt()
function getpt(){
    $.ajax({
        url:config.url + "/cons/getpt",
        dataType:'json',
        xhrFields: {
            withCredentials: true
        },
        data:{

        },
        crossDomain: true,
        type:'GET',
        success:function(resp) {
            renderSelect(resp.data);
        },
        error:function() {
        }
    });
}
function renderSelect(arr){
    let str = ' <option value="">请选择</option>';
    for (let i = 0; i < arr.length; i++) {
        let a  = arr[i];
        str += `<option value="${a.positionalTitles}">${a.positionalTitles}</option>`;
    }
    $('#picker3')[0].innerHTML = str;
    $('#picker5')[0].innerHTML = str;
}
function submitForm(){
    if ($("#input-check1").is(':checked')) {
        $('.none').css('display','flex');
        console.log("咨询师")
        var name = $('#name-inp').val();
        var sch = $('#sch-inp').val();
        if (sch == '' || sch == null){
            sch = '清华大学';
        }
        var zc = $('#picker5').val();
        var bg = $('#bg-inp').val();
        if (bg == '' || bg == null){
            bg = '心理学';
        }
        var intro = $('#intro-inp').val();
        if (intro == '' || intro == null){
            intro = '666';
        }

        if (name == null || name == ''){
            return;
        }
    } else {
        $('.none').css('display','none');
        console.log("管理员")

    }

    $.ajax({
        url:config.url + "/cons/addAdmin",
        dataType:'json',
        xhrFields: {
            withCredentials: true
        },
        data:{
            name:name,
            school:sch,
            positionalTitles:zc,
            professionalBackground:bg,
            introduction:intro
        },
        crossDomain: true,
        type:'POST',
        success:function(resp) {
            renderSelect(resp.data);
        },
        error:function() {
        }
    });
}
function nameBlur(){
    let name = $('#name-inp').val();
    console.log(name);
    if (name == null || name == ''){
        $('#name-tips')[0].innerHTML = '姓名不能为空';
    }else{
        $('#name-tips')[0].innerHTML = '';
    }
}